package com.lemon.smartsafety.ui.adapter.address;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ldzs.recyclerlibrary.swipe.SwipeMenuLayout;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.entity.address.AddrBookUserVo;
import com.lemon.smartsafety.entity.event.address.ContactDeleteEvent;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/7/22 11:15
 * 修改人：Fargo
 * 修改时间：2016/7/22 11:15
 * 修改备注：
 */
public class AddressContactWXRefreshAdapter extends BaseAdapter {

    /**
     * TAG
     */
    private final String TAG = "AddressContactWXRefreshAdapter";

    /**
     * 选择项
     */
    private int m_nSelectItem = -1;

    /**
     * 数据列表
     */
    private List<AddrBookUserVo> mList;


    private AddrBookUserVo mData;

    /**
     * 布局泵
     */
    private LayoutInflater m_layInflater = null;

    private Context mContext;

    private boolean isCheck=false;

    public void setCheck(boolean check) {
        isCheck = check;
    }

    private CallBackInterface mBack = null;

    public interface CallBackInterface {
        public void callBackFunction(AddrBookUserVo data,int item);

    }

    public void setBack(CallBackInterface back) {
        this.mBack = back;
    }


    /**
     * 设置选择项
     *
     * @param n
     */
    public void setSelectItem(final int n) {
        m_nSelectItem = n;
    }

    /**
     * 设置数据列表
     */
    public void setList(ArrayList<AddrBookUserVo> list) {
        this.mList = list;
    }


    public List<AddrBookUserVo> getData() {
        return mList;
    }

    /**
     * 构造函数
     *
     * @param context
     */
    public AddressContactWXRefreshAdapter(Context context, List<AddrBookUserVo> list) {
        try {
            this.mContext = context;

            this.mList = list;

            // 布局泵
            m_layInflater = LayoutInflater.from(context);

        } catch (Exception e) {
        }
    }

    @Override
    public int getCount() {
        try {
            if (mList != null)
                return mList.size();
        } catch (Exception e) {
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        try {
            if (mList != null)
                return mList.get(position);
        } catch (Exception e) {
        }

        return null;
    }



    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings({"deprecation"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = m_layInflater.inflate(R.layout.item_address_book_contact_and_menu_wx, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }


            //内容

            mData = mList.get(position);


            holder.tvTip.setText(mData.getIndexCharI());
            holder.tvTitle.setText(mData.getTitleI());
            holder.tvSubTitle.setText(mData.getSubTitleI());

            if(mData.getHasWeixin()==0){
                holder.txtWx.setVisibility(View.GONE);
            }else{
                holder.txtWx.setVisibility(View.VISIBLE);
            }

            if(isCheck){
                holder.checkBox.setVisibility(View.VISIBLE);
                holder.checkBox.setChecked(mData.isCheck());
                holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        mData.setCheck(isChecked);
                    }
                });

            }else{
                holder.checkBox.setVisibility(View.GONE);
            }



            /*控制色块 */
            if (mData.getResponse_status() == 0) {
                holder.tvStatusTip.setBackgroundColor(holder.tvStatusTip.getContext().getResources().getColor(R.color.colorDisable));
            } else if (mData.getResponse_status() == 1) {
                holder.tvStatusTip.setBackgroundColor(holder.tvStatusTip.getContext().getResources().getColor(R.color.colorAppGreen));
            } else if (mData.getResponse_status() == 2) {
                holder.tvStatusTip.setBackgroundColor(holder.tvStatusTip.getContext().getResources().getColor(R.color.colorAppRed));
            }

            if (StringUtils.getBoolean(mData.getIs_admin() + "")) {
                holder.tvStatusTip.setBackgroundColor(holder.tvStatusTip.getContext().getResources().getColor(R.color.colorPrimary));
            }


            int section = getSectionForPosition(position);
            if (position == getPositionForSection(section)) {
                holder.tvTip.setVisibility(View.VISIBLE);
            } else {
                holder. tvTip.setVisibility(View.GONE);
            }

            holder.txtDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ContactDeleteEvent event = new ContactDeleteEvent();
                    event.setTag(ContactDeleteEvent.class.getName());
                    event.setData(mData);
                    AppService.getBus().post(event);
                }
            });

            holder.ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBack != null) {
                        mBack.callBackFunction(mData, position);
                    }
                }
            });
     /*       holder.ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    if (mBack != null) {
                        mBack.callBackFunction(mData, position);
                    }
                }
            });*/



        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }



    public int getSectionForPosition(int position) {
        return mList.get(position).getIndexCharI().charAt(0);
    }

    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = mList.get(i).getIndexCharI();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }













    static class ViewHolder {
        @Bind(R.id.tv_tip)
        TextView tvTip;
        @Bind(R.id.checkBox)
        CheckBox checkBox;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_sub_title)
        TextView tvSubTitle;
        @Bind(R.id.txt_wx)
        TextView txtWx;
        @Bind(R.id.tv_status_tip)
        TextView tvStatusTip;
        @Bind(R.id.ripple)
        LinearLayout ripple;
        @Bind(R.id.txtDelete)
        TextView txtDelete;
        @Bind(R.id.all)
        SwipeMenuLayout all;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}